package com.ossjk.oa.paid.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.paid.entity.Paid;
import com.ossjk.oa.paid.service.IPaidService;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.service.IEmployeePostService;
import com.ossjk.oa.system.service.IEmployeeService;
import com.ossjk.oa.system.service.IPostService;

@RestController
@RequestMapping("/paid/paid")
public class PaidController extends BaseController {
	@Autowired
	private IEmployeeService iEmployeeService;
	@Autowired
	private IPostService iPostService;
	@Autowired
	private IPaidService iPaidService;
	@Autowired
	private IEmployeePostService iEmployeePostService;

	/**
	 * 获取登录人的薪酬列表
	 * 
	 * @param page
	 * @return
	 */
	@RequiresPermissions("00450005")
	@GetMapping("/listSelf")
	public Object listSelf(String dateStr) {
		Map map = new HashMap<>();
		String eid = getCurrnetUserId();
		Wrapper wrapper = Condition.create().isWhere(true).eq("pa.eid", eid).eq("pa.isdelete", "1");
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
			Date date = simpleDateFormat.parse(dateStr);
			wrapper.eq("pa.date", date);
			map.put("paid", iPaidService.selectVo(wrapper));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Wrapper wrapper2 = Condition.create().eq("eid", eid);
		String pid = iEmployeePostService.selectOne(wrapper2).getPid();
		map.put("employee", iEmployeeService.selectById(eid));
		// 根据部门查找岗位
		map.put("post", iPostService.selectById(pid));
		return responseSuccess(map);
	}

	/**
	 * 获取登录人的薪酬列表
	 * 
	 * @param page
	 * @return
	 */
	@RequiresPermissions("00450010")
	@GetMapping("/listByDid")
	public Object listBydid(String did, String dateStr) {
		Map map = new HashMap<>();
		Wrapper wrapper = Condition.create();
		if (!CommonUtil.isBlank(did)) {
			wrapper.eq("d.id", did);
		}
		// 根据部门查找员工
		List<Employee> employees = iEmployeeService.selectVoList(wrapper);
		map.put("employees", employees);
		// 查找每个员工的薪酬记录
		List<Paid> paids = new ArrayList<>();
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
			Date date = simpleDateFormat.parse(dateStr);
			for (Employee employee : employees) {
				String eid = employee.getId();
				Wrapper wrapper2 = Condition.create().eq("eid", eid).eq("date", date).eq("isdelete", "1");
				Paid paid = iPaidService.selectOne(wrapper2);
				// 有记录
				if (!CommonUtil.isBlank(paid)) {
					paids.add(paid);
				}
			}
			map.put("paids", paids);
			if (!CommonUtil.isBlank(did)) {
				Wrapper wrapper2 = Condition.create().eq("did", did);
				// 根据部门查找岗位
				map.put("posts", iPostService.selectList(wrapper2));
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			employees = null;
			paids = null;
		}
		return responseSuccess(map);
	}
}
